package com.example.testkafka.consumer.config;

import com.example.testkafka.consumer.listener.TestConsumerInterceptor;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ContainerProperties;

import java.util.HashMap;
import java.util.Map;

@PropertySource("classpath:application-kafka.yml")
@Configuration
@EnableKafka
public class KafkaConsumerConfig {

    @Value("${kafka.consumer.bootstrap-servers}")
    private String servers;
    @Value("${kafka.consumer.enable-auto-commit}")
    private boolean enableAutoCommit;
    @Value("${kafka.consumer.group-id}")
    private String groupId;
    @Value("${kafka.consumer.auto-offset-reset}")
    private String autoOffsetReset;
    @Value("${kafka.consumer.concurrency}")
    private int concurrency;
//    @Value("${kafka.consumer.maxPollRecordsConfig}")
//    private int maxPollRecordsConfig;
    @Value("${kafka.consumer.isolation-level}")
    private String isolationLevel;

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        //Spring并发消费,其实是按照此参数为每个带有@KafkaListener注解的方法实例化了concurrency个consumerContainer,每个container初始化了一个consumer.
        factory.setConcurrency(concurrency);
        //此参数决定了消费者消费方法的入参是单条还是多条,必须与之匹配,否则报错
        factory.setBatchListener(false);

        //设置自动提交,不可配置一下手动提交内容
        //==============================手动提交位移配置开始
        //纯手动提交，显示提交并立即执行
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);

        //逐条提交
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.RECORD);

        //批量提交，在下一次poll前,提交所有访问过的消息
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.BATCH);

        //按时提交
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.TIME);
//        factory.getContainerProperties().setAckTime(5000l);

        //按量提交,即每个消费者实例的消费数量达到指定数量后才提交
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.COUNT);
//        factory.getContainerProperties().setAckCount(5);

        //按时或按量提交，消费者实例只要有某一条件满足就提交
//        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.COUNT_TIME);
//        factory.getContainerProperties().setAckCount(5);
//        factory.getContainerProperties().setAckTime(5000L);
//        factory.getContainerProperties().setAckTime(50000L);

        //手动提交，显示提交，下一次poll执行提交
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL);
        //==============================手动提交位移配置结束
        return factory;
    }


    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>(8);
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        //是否自动提交位移
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
        //key反序列化器
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        //value反序列化器
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        //消费群组名称,此处设置,@KafkaListener就可不设置
        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        //消费者找不到消费位移时的读取策略:latest/earliest/none
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        //消费者监听器
        propsMap.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TestConsumerInterceptor.class.getName());
        //事务隔离级别
        propsMap.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, isolationLevel);
        return propsMap;
    }
}
